package com.study.webapp.word;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.TableIterator;

/**
 * https://www.cnblogs.com/libaoting/p/wordToMap.html
 * 
 * @author admin
 *
 *         2018年12月6日
 */
public class WordToHtml {
	// 回车ASCII码
	private static final short ENTER_ASCII = 13;
	// 空格
	private static final short SPACE_ASCII = 32;
	// 水平制表符
	private static final short TABULATION_ASCII = 9;

	public static String htmlText = "";

	public static String inputFile = "D:\\wordlib\\aa.doc";

	private static String filePath = "D:/wordlib/abd.html";

	public static void main(String[] args) {
		try {
			getWord(inputFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void getWord(String fileName) throws Exception {
		FileInputStream in = new FileInputStream(new File(fileName));
		HWPFDocument doc = new HWPFDocument(in);
		// 文档读取范围
		Range range = doc.getRange();
		TableIterator it = new TableIterator(range);
		// 字符总数
		int length = doc.characterLength();

		String htmlText = "<html><head><title>" + doc.getSummaryInformation().getTitle() + "</title></head><body>";
		int cur = 0;
		String tempString = "";
		for (int i = 0; i < length - 1; i++) {
			Range rang = new Range(i, i + 1, doc);
			CharacterRun cr = rang.getCharacterRun(0);
			Range rang2 = new Range(i + 1, i + 2, doc);
			CharacterRun cr2 = rang2.getCharacterRun(0);
			char c = cr.text().charAt(0);
			if (c == SPACE_ASCII) {
				tempString += "&nbsp;";
			}
			boolean flag = compareCharStyle(cr, cr2);
			if (flag && c != ENTER_ASCII) {
				tempString += cr.text();
			} else {
				String fontStyle = "<span style='font-family:" + cr.getFontName() + ";font-size:" + cr.getFontSize() / 2
						+ "pt;color:" + getHexColor(cr.getIco24()) + ";";
				if (cr.isBold()) {
					fontStyle += "font-weight:bold";
				}
				if (cr.isItalic()) {
					fontStyle += "font-style:italic";
				}
				htmlText += fontStyle + "'>" + tempString + cr.text();
				htmlText += "</span>";
				tempString = "";
			}
			if (c == ENTER_ASCII) {
				htmlText += "<br/>";
			}

		}
		htmlText += tempString + "</body></html>";
		writeFile(htmlText);
		System.out.println("----------成功---------");

	}

	public static void writeFile(String s) {
		FileOutputStream fos = null;
		BufferedWriter bw = null;
		PrintWriter writer = null;
		try {
			File file = new File(filePath);
			fos = new FileOutputStream(file);
			bw = new BufferedWriter(new OutputStreamWriter(fos));
			bw.write(s);

			writer = new PrintWriter(file, "UTF-8");
			writer.write(s);
			writer.flush();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (bw != null) {
					bw.close();
				}
				if (fos != null) {
					fos.close();
				}
				if (writer != null) {
					writer.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static boolean compareCharStyle(CharacterRun cr1, CharacterRun cr2) {
		boolean flag = false;
		boolean f = cr1.isBold() == cr2.isBold() && cr1.isItalic() == cr2.isItalic()
				&& cr1.getFontName().equals(cr2.getFontName()) && cr1.getFontSize() == cr2.getFontSize()
				&& cr1.getColor() == cr2.getColor();
		if (f) {
			flag = true;
		}
		return flag;
	}

	public static String getHexColor(int color) {
		color = color == -1 ? 0 : color;
		int rgb = rgb(color);
		return "#" + rgbToSix(Integer.toHexString(rgb));
	}

	public static int rgb(int c) {
		return (c >> 16) & 0XFF;
	}

	public static String rgbToSix(String rgb) {
		int length = 6 - rgb.length();
		String str = "";
		while (length > 0) {
			str += "0";
			length--;
		}
		return str + rgb;
	}

}
